package app;

import java.util.regex.Pattern;

import javax.swing.JOptionPane;

public class Analizador {

	
	
String lineas[];


private static boolean imageshack(String str) {
	if (str.contains("imageshack.us")) {
		JOptionPane.showMessageDialog( null,"No se permiten imagenes de IMAGESHACK","Epa!", JOptionPane.INFORMATION_MESSAGE );
		return true;
	}
	return false;
}
	
	public static boolean esLink(String str2) {
		String str=str2.toLowerCase();
		if (str.startsWith("http://") | str.startsWith("www.")  | str.startsWith("ftp://") | str.startsWith("https://")) return true;
		
		return false;
	}
	
	public static boolean esImagen(String str2) {
		String str=str2.toLowerCase();
	
	if (esLink(str))  {
		
			if (str.endsWith(".jpg") | str.endsWith(".png") | str.endsWith(".gif")  | str.endsWith(".jpeg") ) 	return true; 
	}			
	
	
	return false;
		

}
	
	public static boolean esVideo(String str2) {
		String str=str2.toLowerCase();
		if (esLink(str) && str.contains("youtube.com/watch"))
				return true;
		return false;
	}
		
	public static boolean esDailyMotion(String str2) {
		String str=str2.toLowerCase();
		if (esLink(str) && str.contains("dailymotion.com/video"))
				return true;
		return false;
	}
	
	

	public static boolean esGvideo(String str2) {
		String str=str2.toLowerCase();
		if (esLink(str) && str.contains("video.google.com/videoplay"))
				return true;
		return false;
	}


	public static boolean esVimeo(String str2) {
		String str=str2.toLowerCase();
		if (esLink(str) && str.contains("vimeo.com/"))
				return true;
		return false;
	}


	

	public static String agregarImagen(String str,boolean center) {
		System.out.println(str);
		/*if (imageshack(str)) return "";
		else*/		
		if (center) return ("[align=center][img="+str+"][/align]");
		else return ("[img="+str+"]");
		
				
	}

public static String agregarVideo(String str,boolean center,boolean link) {
	//    http://www.youtube.com/watch?v=XfFSHR9YbZg
		//[video]http://www.youtube.com/watch?feature=player_detailpage&v=TVGWFbLOzpw[/video]
	try {
		int inicio=str.indexOf("v=")+2;
		int fin=str.indexOf('&',inicio);
		if (fin==-1) fin=str.length();
		String substring = str.substring(inicio,fin);
		
		
		if (link) return "[align=center][video]http://www.youtube.com/watch?feature=player_detailpage&v="+substring+"[/video]\nlink: [url=http://www.youtube.com/watch?feature=player_detailpage&v="+substring+"]http://www.youtube.com/watch?v="+substring+"[/url][/align]";
		else return "[align=center][video]http://www.youtube.com/watch?feature=player_detailpage&v="+substring+"[video][/align]";
	}
	catch (Exception E) {
		return "";
	}
	}

public static String agregarSwf(String str,boolean link) {
	
		if (link) return "[swf="+str+"]\nLink: [url="+str+"]"+str+"[/url]";
		else return "[swf="+str+"]";
			
}

public static String agregarLink(String link,String texto) {
	
	
		if (link.equals(texto)) return "[url="+link+"]"+link+"[/url]";
		else return "[url="+link+"]"+texto+"[/url]";
	
		
}

public static String agregarImagenConLink(String link,String imagen,boolean centrado) {
	
	
	if (!centrado) return "[url="+link+"][img="+imagen+"][/url]";
	else return "[align=center][url="+link+"][img="+imagen+"][/url][/align]";
		
}



public static String agregarQuote(String str) {
	
	return "[quote] "+str+" [/quote]";
}


public static String agregarGvideo(String str,boolean link) {
	try {
	int inicio=str.indexOf('?')+1;
	int fin=str.length()-1;
	String substring = str.substring(inicio,fin);
	if (link) return "[align=center][swf=http://video.google.com/googleplayer.swf?"+substring+"]\nLink: [url=http://video.google.com/googleplayer.swf?"+substring+"]http://video.google.com/googleplayer.swf?"+substring+"[/url][/align]\n";
	else return "[align=center][swf=http://video.google.com/googleplayer.swf?"+substring+"][/align]\n";
	}
	catch (Exception E)
	{
		return "";
	}
}



public static String agregarVimeo(String str2,boolean link) {
	String str=str2.toLowerCase();
	String substring = "";
	
	if (str.startsWith("<iframe src")) {
		try {
			int inicio=str2.indexOf("video/")+6;
			int fin=str2.indexOf("\" ");
			substring = str2.substring(inicio,fin);}
	
		catch (Exception E)
		{
			return "";
		}
	}
	if (str.startsWith("http://")) {

		try {
			int inicio=str2.indexOf("com/")+4;
			int fin=str2.length();
			substring = str2.substring(inicio,fin);
		
		
		}
		
		catch (Exception E)
			{
				return "";
			}		
		
		
		
	
	}
	if (link) return "[align=center][swf=http://a.vimeocdn.com/p/flash/moogaloop/5.1.2/moogaloop.swf?clip_id="+substring+"]\nLink: [url="+substring+"]http://vimeo.com/"+substring+"[/url][/align]\n";
	else
		 return "[align=center][swf=http://a.vimeocdn.com/p/flash/moogaloop/5.1.2/moogaloop.swf?clip_id="+substring+"][/align]\n";
		

	
}



public static boolean esSwf(String str2) {
	String str=str2.toLowerCase();
	if (esLink(str) && str.contains(".swf"))
			return true;
	return false;
}
	


public  static String agregarGoear(String str,boolean link) {
	
	//http://www.goear.com/listen/bfd6ab6/whatever-you-like-t.i.
	//[swf]http://www.goear.com/files/external.swf?file=bfd6ab6[/swf]
	
	try {
		
	
	int inicio=str.indexOf("listen/")+7;
	int fin = str.indexOf("/", inicio+4);
	String substring = str.substring(inicio,fin);
	if (link) return "[align=center][swf]http://www.goear.com/files/external.swf?file="+substring+"[/swf]\nLink: [url="+str+"]"+str+"[/url][/align]\n";
	else return "[align=center][swf]http://www.goear.com/files/external.swf?file="+substring+"[/swf][/align]\n";
	}
	
	catch (Exception E) {
		return "";
	}
}



public static boolean esMetacafe(String str2) {
	String str=str2.toLowerCase();
	if (esLink(str) && str.contains("metacafe.com/watch"))
			return true;
	return false;
}


public static String agregarMegavideo(String str,boolean link) {
	
	//http://megavideo.com/?v=UACGTMF7
	//[swf]http://www.megavideo.com/v/UACGTMF7[/swf]
	try {
	int inicio=str.indexOf("?v=")+3;
	int fin=str.length();
	String substring = str.substring(inicio,fin);
	if (link) return "[align=center][swf]http://www.megavideo.com/v/"+substring+"[/swf]\n"+"Link: [url="+str+"]"+str+"[/url][/align]\n";
	else return "[align=center][swf]http://www.megavideo.com/v/"+substring+"[/swf][/align]\n";
	}
	catch (Exception E) {
		return "";
		
	}
		
}

public static boolean esGoear(String str2) {
	String str=str2.toLowerCase();
	if (esLink(str) && str.contains("goear.com/listen/"))
			return true;
	return false;
}




public static String generaFuente(String str)
{
	if (str==null) str="";
	return("[url="+str+"]Fuente[/url]"+"\n");
	
	
}


public static String agregarDailyMotion(String str, boolean link){
	int  inicio=str.indexOf("/video/")+7;
	int fin=str.indexOf('_');
	String substring = str.substring(inicio,fin);
	if (link) return("[align=center]"+agregarSwf("http://www.dailymotion.com/swf/video/"+substring,false)+"\nLink: [url=http://www.dailymotion.com/video/"+substring+"]"+str+"[/url][/align]\n");
	else return("[align=center]"+agregarSwf("http://www.dailymotion.com/swf/video/"+substring,false)+"[/align]\n");
}

public static String agregarMetacafe(String str, boolean link){
	int  inicio=str.indexOf("/watch/")+7;
	int fin=str.length();
	String substring = str.substring(inicio,fin-1);
	if (link) return("[align=center]"+agregarSwf("http://www.metacafe.com/fplayer/"+substring+".swf",false)+"\nLink: [url="+str+"]"+str+"[/url][/align]\n");
	else return("[align=center]"+agregarSwf("http://www.metacafe.com/fplayer/"+substring+".swf",false)+"[/align]\n");
}


public static String agregarBliptv(String str,boolean link){
	
	
	
	
	int inicio=str.indexOf("<embed src=")+12;
	int fin=str.indexOf('"',inicio);
	
	String substring = str.substring(inicio,fin);
	return("[align=center]"+agregarSwf(substring,link)+"[/align]\n");
	


}


public static boolean esBliptv(String str2) {
	String str=str2.toLowerCase();
	if (str.contains("http://blip.tv/play/"))
		{System.out.println("blip");
			return true;}
	return false;
}


/*
 * 
 * Generacion automatica
 * 
 */





public static String convierte(String str,boolean imgCentradas,boolean links,boolean vueltaCarro,boolean acortar) {

	
	String salida="",palabra="",parrafo="";
	
	String[] Arr_parrafos = Pattern.compile("\n").split(str);
	
	
	for (int i=0; i<Arr_parrafos.length; i++) {
		int ultimoParrafo=Arr_parrafos.length-1;
	    parrafo=Arr_parrafos[i];  
	    String[] Arr_palabras = Pattern.compile(" ").split(parrafo);
		for (int j=0; j<Arr_palabras.length; j++) {
			palabra=Arr_palabras[j];
			
			if (esImagen(palabra)){
				
				if (!(imageshack(palabra))) {
				if (imgCentradas) salida=salida + agregarImagen(palabra,true);
					else salida=salida + agregarImagen(palabra,false);						
				}
			}
	            else if(esVimeo(palabra)) salida=salida +agregarVimeo(palabra,links);
	            	else if(esVideo(palabra)) salida=salida + agregarVideo(palabra,false,links);
	            		else if(esGvideo(palabra)) salida=salida + agregarGvideo(palabra,links);
							else if(esMetacafe(palabra)) salida=salida + agregarMetacafe(palabra,links);
								else if(esDailyMotion(palabra)) salida=salida + agregarDailyMotion(palabra,links);
									//	else if (esBliptv(palabra)) salida=salida + agregarBliptv(palabra);
										else if(esSwf(palabra))salida=salida+ agregarSwf(palabra,links);
											else if (esGoear(palabra)) salida=salida + agregarGoear(palabra,links);
												else if (esLink(palabra)) salida=salida+agregarLink(palabra,palabra);
													else salida=salida+palabra+" ";
			
		}
		
		if (vueltaCarro && i!=ultimoParrafo) salida=salida+"\n";
		   
		
	}
	


return salida;	
}


public static String generatitulo(String titulo){
	
	return ("[align=center][color=orange][size=18][font=Trebuchet MS]"+titulo+"[/font][/size][/color][/align]"+"\n\n"+"[align=center][img=http://k03.kn3.net/0AFB8F4A9.png][/align]\n");

}
}
